package org.springblade.common.utils.smsutil;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MessageUtil {


    public synchronized static void sendBatchMessage(String content, String mobiles) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String timetamp = sdf.format(new Date());

        URL wsUrl = new URL("https://sdk2.028lk.com/sdk2/LinkWS.asmx");
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
        OutputStream os = conn.getOutputStream();
        //请求体
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                "  <soap12:Body>\n" +
                "    <BatchSend2 xmlns=\"http://tempuri.org/\">\n" +
                "      <CorpID>LC010903</CorpID>\n" +
                "      <Pwd>jnlgb0720!</Pwd>\n" +
                "      <Mobile>" + mobiles + "</Mobile>\n" +
                "      <Content>" + content + "</Content>\n" +
                "      <Cell></Cell>\n" +
                "      <SendTime>" + timetamp + "</SendTime>\n" +
                "    </BatchSend2>\n" +
                "  </soap12:Body>\n" +
                "</soap12:Envelope>";
        os.write(soap.getBytes());
        InputStream is = conn.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while ((len = is.read(b)) != -1) {
            String ss = new String(b, 0, len, "UTF-8");
            s += ss;
        }
        System.out.println(s);
        System.out.println("发送成功！");
        is.close();
        os.close();
        conn.disconnect();

    }
//
//    public static void main(String[] args) {
//        try {
//            sendBatchMessage(MessageTemplate.smsLoginUpwdRegist("666777"), "17615219987,15253155396");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
